package main

import "image"

var mousecoords image.Point
var scroll int

func domouse() {
	mc := disp.InitMouse()
	for {
		select {
		case m := <-mc.C:
			switch m.Buttons {
			case 1: // left click
				mousecoords = image.Pt(m.X, m.Y)
				redraw(false)
			case 8:
				scrollUp(1)
			case 16:
				scrollDown(1)
			}
		case <-mc.Resize:
			redraw(true)
		}
	}
}

func dokb() {
	kc := disp.InitKeyboard()
	for {
		key := <-kc.C
		switch key {
		case 27: // esc
			return
		case 61454: // up
			scrollUp(18)
		case 128: // down
			scrollDown(18)
		case 61455: // pg up
			scrollUp(40)
		case 61459: // pg dn
			scrollDown(40)
		}
	}
}

func scrollUp(factor int) {
	top := 0
	if scroll-factor > top {
		scroll -= factor
	} else if scroll != top {
		scroll = top
	}
	redraw(false)
}

func scrollDown(factor int) {
	bottom := dot - (screen.R.Max.Y / disp.DefaultFont.Height)
	if scroll+factor < bottom {
		scroll += factor
	} else if scroll != bottom {
		scroll = bottom
	}
	redraw(false)
}
